package com.huawei.hwid.api.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.LoginHandler;
import com.huawei.cloudservice.c;
import com.huawei.cloudservice.c.a;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.n;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.helper.handler.ErrorStatus;

public class l extends b {
    private String d;
    private Bundle e;
    private LoginHandler f;
    private Context g;

    public l(Context context, String str, Bundle bundle, LoginHandler loginHandler) {
        super(context);
        this.g = context;
        this.d = str;
        this.e = bundle;
        this.f = loginHandler;
    }

    void b() {
        a a = a.a(this.g);
        if (a != null) {
            try {
                a.a().a(this.d, this.e, a(this.f));
            } catch (RemoteException e) {
                e.b("LoginTask", "login remote exception", true);
            }
        }
    }

    void a(ErrorStatus errorStatus) {
        if (errorStatus == null) {
            errorStatus = new ErrorStatus(39, "login timeout. retry again");
        }
        this.f.onError(errorStatus);
    }

    private c a(final LoginHandler loginHandler) {
        return new a(this) {
            final /* synthetic */ l b;

            public void a(int i, Bundle bundle) throws RemoteException {
                if (this.b.b.get()) {
                    e.b("LoginTask", "has cancelled by timeout, return directly", true);
                    return;
                }
                this.b.a();
                if (i == -1) {
                    HwAccount a = new HwAccount().a(bundle);
                    this.b.a(a);
                    com.huawei.hwid.d.a.a(this.b.g).a(a);
                    CloudAccount[] a2 = e.a(this.b.g);
                    String str = "";
                    if (!TextUtils.isEmpty(a.b())) {
                        str = a.b();
                    }
                    e.b("LoginTask", "loginResult", true);
                    loginHandler.onLogin(a2, e.a(a2, str));
                    com.huawei.hwid.c.a.a(this.b.g).a(this.b.g, a);
                } else if (i == 0) {
                    loginHandler.onError(new ErrorStatus(31, "Account hasnot login"));
                } else if (i == 1) {
                    loginHandler.onError(new ErrorStatus(29, "Signature invalid"));
                } else if (i == 2) {
                    loginHandler.onError(new ErrorStatus(30, "serviceToken invalid"));
                } else {
                    e.b("LoginTask", "DONT KNOW RET_CODE:", true);
                }
            }

            public void a(int i, Intent intent) throws RemoteException {
            }

            public void a(int i) throws RemoteException {
            }

            public void b(int i, Bundle bundle) throws RemoteException {
            }
        };
    }

    private void a(HwAccount hwAccount) {
        Object q = hwAccount.q();
        if (TextUtils.isEmpty(q) || HwAccountConstants.NULL.equalsIgnoreCase(q)) {
            String a = n.a(this.g, 0);
            if (a == null) {
                a = "";
            }
            hwAccount.p(a);
        }
    }

    public String toString() {
        return "LoginTask{mServiceType='" + this.d + '\'' + '}';
    }
}
